//
//  NRWCarChooseItemView.m
//  New_Runway
//
//  Created by NewLife on 2018/3/16.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarChooseItemView.h"

CGFloat baseTag = 123;

@implementation NRWCarChooseItemView
{
    UIButton *_lastClickBtn;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
    
        [self setDefaultVaulue];
    }
    return self;
}

- (void)prepare {
    
    [self setupUI];
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    
    _selectIndex = selectIndex;
    [self  buttonResetState];
    UIButton *btn = [self viewWithTag:selectIndex + baseTag];
    btn.selected = YES;
}
-(void)buttonResetState{
    for (int i = 0; i < self.itemTitleArr.count; i++) {
        UIButton *btn = [self viewWithTag:i + baseTag];
        btn.selected = NO;
    }
}
- (void)setDefaultVaulue {
    
    self.itemNumInRow = 3;
    self.itemHeight = 40;
    self.margin = 16;
    self.marginSides = 16;
    self.marginUpDown = 16;
    self.titleColor = getColorFromString(@"323232");
    self.selectTitleColor = self.titleColor;
    self.titleFont = kPingFangRegular13();
    self.itemBackColor = [UIColor whiteColor];
    self.itemSelectBackColor = self.itemBackColor;
    self.showBorder = YES;
    self.borderColor = getColorFromString(@"989898");
    self.borderWidth = 0.5;
    self.cornerRadius = 0;
}

- (void)setupUI {
    
    if (self.itemTitleArr.count == 0) {
        return;
    }
    
    for (NSInteger i = 0; i < self.itemTitleArr.count; i++) {
        
        UIButton *btn = DPButton()
        .setNormalTitle(self.itemTitleArr[i])
        .setNormalTitleColor(self.titleColor)
        .setSelectedTitleColor(self.selectTitleColor)
        .setTitleFont(self.titleFont)
        .setNormalBackgroundColor(self.itemBackColor)
        .setSelectedBackgroundColor(self.itemSelectBackColor)
        .setTag(baseTag + i)
        .addClickAction(self, @selector(btnDidClick:));
        self.addSubview(btn);
        
        if (!isEmptyOrNilString(self.normalImage)) {
            [btn setBackgroundImage:getImageFromName(self.normalImage) forState:UIControlStateNormal];
        }
        if (!isEmptyOrNilString(self.selectImage)) {
            [btn setBackgroundImage:getImageFromName(self.selectImage) forState:UIControlStateSelected];
        }
        
        if (self.showBorder) {
            
            btn.layer.borderColor = self.borderColor.CGColor;
            btn.layer.borderWidth = self.borderWidth;
            btn.layer.cornerRadius = self.cornerRadius;
        }
        
        NSInteger rowItemIndex = i % self.itemNumInRow;
        NSInteger rowNum = i / self.itemNumInRow;
        CGFloat itemWidth = (Screen_width - 2 * self.marginSides - (self.itemNumInRow - 1) * self.margin) / self.itemNumInRow;
        
        btn.frame = CGRectMake(self.marginSides + rowItemIndex * (itemWidth + self.marginSides), rowNum * (self.itemHeight + self.marginUpDown) , itemWidth, self.itemHeight);
    }
}

- (void)btnDidClick:(UIButton *)sender {
    
    if (_lastClickBtn == sender) {
        return;
    }
    
    sender.selected = YES;
    _lastClickBtn.selected = NO;
    _lastClickBtn = sender;
    
    if ([self.delegate respondsToSelector:@selector(chooseItemView:itemDidClickWithItem:andItemIndex:)]) {
        
        [self.delegate chooseItemView:self itemDidClickWithItem:sender andItemIndex:sender.tag - baseTag];
    }
}

- (CGFloat)chooseViewHeight {
    
    CGFloat rowNum = ((CGFloat)self.itemTitleArr.count / (CGFloat)self.itemNumInRow) == 0? 1: ((CGFloat)self.itemTitleArr.count / (CGFloat)self.itemNumInRow);
    
    if (rowNum > (int)rowNum) {
        rowNum += 1;
    }
    
    return self.itemHeight + (rowNum - 1) * (self.itemHeight + self.marginUpDown);
}

@end
